package com.hkyl.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.entity.*;
import com.hkyl.hospital.mapper.DeptMapper;
import com.hkyl.hospital.mapper.DoctorSortMapper;
import com.hkyl.hospital.mapper.UserMapper;
import com.hkyl.hospital.service.DoctorSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Transactional
public class DoctorSortServiceImpl implements DoctorSortService {
    @Autowired
    private DoctorSortMapper doctorSortMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public PageInfo<DoctorSort> selectAll(Map map) {
        List<Object> weeks = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
        Integer week = (!weeks.contains(map.get("week"))) ? null : (Integer) map.get("week");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        Integer deptId =(map.get("deptId") == null) ? null : (Integer) map.get("deptId");
        String doctorName = (String) map.get("doctorName");
        List<Integer> doctorIds = new ArrayList<>();
        if (StringUtils.hasText(doctorName)) {
            UserExample userExample = new UserExample();
            userExample.or().andRealNameLike("%" + doctorName + "%");
            List<User> users = userMapper.selectByExample(userExample);
            doctorIds = users.stream().map(user -> user.getUserId()).collect(Collectors.toList());
        }
        DoctorSortExample doctorSortExample = new DoctorSortExample();
        if (week != null) {
            doctorSortExample.or().andWeekEqualTo(week);
        }
        if (deptId != null) {
            doctorSortExample.or().andDeptIdEqualTo(deptId);
        }
        if (doctorIds.size()==0 && StringUtils.hasText(doctorName)) {
            return null;
        }
        if (doctorIds.size()!=0){
            doctorSortExample.or().andDoctorIdIn(doctorIds);
        }
        List<DoctorSort> doctorSorts = doctorSortMapper.selectByExample(doctorSortExample);
        PageInfo<DoctorSort> pageInfo = new PageInfo<>(doctorSorts);
        setUserDoctorAndDept(pageInfo.getList());
        return pageInfo;
    }

    @Override
    public int update(DoctorSort doctorSort) {
 /*       Integer doctorId = doctorSort.getDoctorId();
        DoctorSortExample doctorSortExample = new DoctorSortExample();
        doctorSortExample.or().andDoctorIdEqualTo(doctorId);
        List<DoctorSort> doctorSorts = doctorSortMapper.selectByExample(doctorSortExample);
        System.out.println("doctorSorts = " + doctorSorts);
        if (doctorSorts.size()==0) {
            doctorSortMapper.updateByPrimaryKeySelective(doctorSort);
        }
        List<Integer> weeks = doctorSorts.stream().map(d -> d.getWeek()).collect(Collectors.toList());
        System.out.println("weeks = " + weeks);
        if (weeks.contains(doctorSort.getWeek())) {
            doctorSortMapper.updateByPrimaryKeySelective(doctorSort);
        }*/
        int num = excludeDoctorSort(doctorSort);
        return num == 0 ? doctorSortMapper.updateByPrimaryKeySelective(doctorSort) : num;
    }

    @Override
    public Map findAll() {
        List<DoctorSort> doctorSorts = doctorSortMapper.selectByExample(null);
        setUserDoctorAndDept(doctorSorts);
        Map<Dept, Map<Integer, List<DoctorSort>>> map = doctorSorts.stream().collect(Collectors.groupingBy(
                doctorSort -> doctorSort.getDept(),
                Collectors.groupingBy(doctorSort -> doctorSort.getWeek())
        ));
        return map;
    }
    @Override
    public int save(DoctorSort doctorSort) {
/*        Integer doctorId = doctorSort.getDoctorId();
        DoctorSortExample doctorSortExample = new DoctorSortExample();
        doctorSortExample.or().andDoctorIdEqualTo(doctorId);
        List<DoctorSort> doctorSorts = doctorSortMapper.selectByExample(doctorSortExample);
        if (doctorSorts.size()==0) {
            return doctorSortMapper.insertSelective(doctorSort);
        }
        List<Integer> weeks = doctorSorts.stream().map(d -> d.getWeek()).collect(Collectors.toList());
        if (weeks.contains(doctorSort.getWeek())) {
            return doctorSortMapper.insertSelective(doctorSort);
        }*/
        int num = excludeDoctorSort(doctorSort);
        return num == 0 ? doctorSortMapper.insertSelective(doctorSort) : num;
    }


    @Override
    public int delete(Integer id) {
        return doctorSortMapper.deleteByPrimaryKey(id);
    }

    public void setDept(List<Dept> depts) {
        List<DoctorSort> doctorSorts = doctorSortMapper.selectByExample(null);
        setUserDoctorAndDept(doctorSorts);
        for (DoctorSort doctorSort : doctorSorts) {
            switch (doctorSort.getWeek()) {
                case 1:
                    depts.stream().filter(dept -> dept.getDeptId()==doctorSort.getDeptId()).forEach(dept -> dept.getOne().add(doctorSort.getDoctor()));
                    break;
                case 2:
                    depts.stream().filter(dept -> dept.getDeptId()==doctorSort.getDeptId()).forEach(dept -> dept.getTwo().add(doctorSort.getDoctor()));
                    break;
                case 3:
                    depts.stream().filter(dept -> dept.getDeptId()==doctorSort.getDeptId()).forEach(dept -> dept.getThree().add(doctorSort.getDoctor()));
                    break;
                case 4:
                    depts.stream().filter(dept -> dept.getDeptId()==doctorSort.getDeptId()).forEach(dept -> dept.getFour().add(doctorSort.getDoctor()));
                    break;
                case 5:
                    depts.stream().filter(dept -> dept.getDeptId()==doctorSort.getDeptId()).forEach(dept -> dept.getFive().add(doctorSort.getDoctor()));
                    break;
                case 6:
                    depts.stream().filter(dept -> dept.getDeptId()==doctorSort.getDeptId()).forEach(dept -> dept.getSix().add(doctorSort.getDoctor()));
                    break;
                case 7:
                    depts.stream().filter(dept -> dept.getDeptId()==doctorSort.getDeptId()).forEach(dept -> dept.getSeven().add(doctorSort.getDoctor()));
                    break;
            }
        }
    }

    private int excludeDoctorSort(DoctorSort doctorSort) {
        DoctorSortExample doctorSortExample = new DoctorSortExample();
        doctorSortExample.or().andDoctorIdEqualTo(doctorSort.getDoctorId()).andWeekEqualTo(doctorSort.getWeek());

        List<DoctorSort> doctorSorts = doctorSortMapper.selectByExample(doctorSortExample);
        if (doctorSorts.size() == 0) {
            return 0;
        }
        return -1;
    }
    private void setUserDoctorAndDept(List<DoctorSort> doctorSorts) {
        List<User> users = userMapper.selectByExample(null);
        List<Dept> depts = deptMapper.selectByExample(null);
        for (DoctorSort doctorSort : doctorSorts) {
            for (User user : users) {
                if (user.getUserId() == doctorSort.getDoctorId()) {
                    doctorSort.setDoctor(user);
                }
            }
            for (Dept dept : depts) {
                if (dept.getDeptId() == doctorSort.getDeptId()) {
                    doctorSort.setDept(dept);

                }
            }
        }
    }
}
